//
//  BusnessAuthorViewController.m
//  BFoodSystem
//
//  Created by 浙江择富 on 2018/1/17.
//  Copyright © 2018年 陈名正. All rights reserved.
//

#import "BusnessAuthorViewController.h"
#import "BFBusinessAuthorTableViewCell.h"
#import "BFBusinessHeadView.h"
#import "BFUpImageController.h"
#import "BFBusinessAuthorServices.h"
#import "takePhoto.h"
#import "BFAuthorAServicces.h"
@interface BusnessAuthorViewController ()<UITableViewDelegate,UITableViewDataSource,MycellDelegate,BFUpImageControllerDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) NSArray *nameArr;
@property (nonatomic, strong) UIButton *commitBtn;
@property (nonatomic, strong)BFBusinessHeadView *BFHeadView;
@property (nonatomic, strong) BFBusinessAuthorTableViewCell *cell;
@property (nonatomic,strong) NSArray *topArr;
@property (nonatomic,strong) NSArray *footArr;
@property (nonatomic,strong) UIImageView *upImage;

@property (nonatomic,strong) NSMutableDictionary *imgDic;

@end

@implementation BusnessAuthorViewController


- (NSArray *)nameArr{
    if (!_nameArr) {
        _nameArr = @[@"身份证正面：",@"身份证反面：",@"营业执照：",@"特殊行业许可证：",@"店面门头照：",@"店内前台场景照：",@"店内营业场景照：",@"店内营业场景照：",@"公司结算账户授权书：",@"移动支付业务商户协议书："];
    }
    return _nameArr;
}

//**区头视图*/
- (UIView *)headerView
{
    if (!_headerView)
    {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH-20, 60*4)];
         _headerView.backgroundColor = [UIColor whiteColor];
    }
    return _headerView;
}

//**区脚视图*/
- (UIView *)footerView
{
    if (!_footerView)
    {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 8*60+45)];
        _footerView.backgroundColor = [UIColor whiteColor];
    }
    return _footerView;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        CGFloat tabViewY = 20;
        if (IS_IPhone5) {
            tabViewY = 10;
        }
        _tableView = [[UITableView alloc]init];
//        _tableView.frame = CGRectMake(20, 20, SCREEN_WIDTH-40, SCREEN_HEIGHT-40);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
//        _tableView.rowHeight = 180;
        [self.tableView registerClass:[BFBusinessAuthorTableViewCell class] forCellReuseIdentifier: BFBusinessAuthorCellIndetifier];
    }
    
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.title  = @"商家认证";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(NavBarHeight+10);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.bottom.equalTo(self.view).offset(-20);

    }];
    
    
    [self headView];
    [self footView];
    
    _imgDic = [[NSMutableDictionary alloc] init];

}
- (NSArray *)topArr{
    if (!_topArr) {
    _topArr = @[@"真实名称：",@"商户简称：",@"身份证号码：",@"详细地址："];
    }
    
    return _topArr;
}
//区头视图
- (void)headView{
   
    NSArray *placeHolderArr = @[@"请输入真实名称",@"请输入商户简称",@"请输入身份证号",@"请输入详细地址"];
    
    for (int i = 0; i<self.topArr.count; i++) {

        _BFHeadView = [[BFBusinessHeadView alloc] initWithFrame:CGRectMake(0, i*60, SCREEN_WIDTH-20*2, 60)];
        _BFHeadView.tag = i+20;
        _BFHeadView.nameLab.text = self.topArr[i];
        _BFHeadView.tf.placeholder = placeHolderArr[i];
        [self.headerView addSubview:_BFHeadView];
    }

    self.tableView.tableHeaderView = self.headerView;
    
    
}
- (NSArray *)footArr{
    
    if (!_footArr) {
         _footArr= @[@"营业执照号：",@"银行卡号：",@"开户姓名：",@"开户支行：",@"手机号：",@"邮箱号："];
    }
    return _footArr;
}
//区脚视图
- (void)footView{

    NSArray *placeArr = @[@"请输入营业执照号",@"请输入银行卡号",@"请输入开户姓名",@"请输入开户行",@"请输入手机号",@"请输入邮箱"];
    
    for (int i = 0; i<self.footArr.count; i++) {
        
       _BFHeadView = [[BFBusinessHeadView alloc] initWithFrame:CGRectMake(0, i*60, SCREEN_WIDTH-20*2, 60)];
        _BFHeadView.nameLab.text = self.footArr[i];
        _BFHeadView.tf.placeholder = placeArr[i];
        _BFHeadView.tag = 100+i;
        [self.footerView addSubview:_BFHeadView];
    }
  

    self.commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.commitBtn setTitle:@"提交" forState:UIControlStateNormal];
    self.commitBtn.backgroundColor = [UIColor redColor];
    self.commitBtn.layer.cornerRadius = 8;
    [self.commitBtn setTintColor:[UIColor colorWithHex:BF_COLOR_B0]];
    [self.footerView addSubview:self.commitBtn];
    [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.footerView).offset(-45);
        make.left.equalTo(self.footerView).offset(20);
         make.right.equalTo(self.footerView).offset(-20);
        make.height.mas_equalTo(60);
    }];
    [self.commitBtn addTarget:self action:@selector(commitBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableFooterView = self.footerView;
}
   //**提交审核认证信息*/
- (void)commitBtn:(UIButton *)btn{
    [self.view endEditing:YES];
    //区头View
    BFBusinessHeadView *nickView =[self.headerView viewWithTag:20];
    BFBusinessHeadView *shopView =[self.headerView viewWithTag:21];
    BFBusinessHeadView *idenfierView =[self.headerView viewWithTag:22];
    BFBusinessHeadView *adressView =[self.headerView viewWithTag:23];
//区脚View
    BFBusinessHeadView *licenView =[self.footerView viewWithTag:100];
    BFBusinessHeadView *bankNumView =[self.footerView viewWithTag:101];
    BFBusinessHeadView *bankNameView =[self.footerView viewWithTag:102];
    BFBusinessHeadView *bankAdressView =[self.footerView viewWithTag:103];
    BFBusinessHeadView *phoneView =[self.footerView viewWithTag:104];
    BFBusinessHeadView *mailView =[self.footerView viewWithTag:105];
    
    if (!nickView.tf.text.length ||!shopView.tf.text.length||!idenfierView.tf.text.length||!adressView.tf.text.length||!licenView.tf.text.length||!bankNumView.tf.text.length||!bankNameView.tf.text.length||!bankAdressView.tf.text.length||!phoneView.tf.text.length||!mailView.tf.text.length) {
          [BFUtils showAlertController:1 title:@"" message:@"请输入完整的信息"];
    }
    else if (![phoneView.tf.text isPhoneNumber]) {
        [BFUtils showAlertController:1 title:@"" message:@"请输入正确的手机号"];
    }
    else if (idenfierView.tf.text.length != 18) {
         [BFUtils showAlertController:1 title:@"" message:@"请输入正确的身份证号"];
    }
    else if (licenView.tf.text.length != 15 ||licenView.tf.text.length != 18) {
        [BFUtils showAlertController:1 title:@"" message:@"请输入正确的营业执照号"];
    }
        else{
    
    //添加数据
    [[BFBusinessAuthorServices alloc] postMerchatInfoWithTruename:nickView.tf.text merchantName:shopView.tf.text sfznumber:idenfierView.tf.text address:adressView.tf.text license:licenView.tf.text blank_number:bankNumView.tf.text blank_name:bankNameView.tf.text blank_address:bankAdressView.tf.text phone:phoneView.tf.text mail:mailView.tf.text SuccessBlock:^(id result) {
         [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
  
        UIAlertController * alert = [BFUtils alertController:nil message:result];
        UIAlertAction * confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alert addAction:confirm];
    } errorCode:^(NSInteger errorCode, NSString *errorMessage) {
        
    } Failure:^(NSError *error) {
        
    }];
}
    
}
#pragma 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.nameArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//           BFBusinessAuthorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BFBusinessAuthorCellIndetifier forIndexPath:indexPath];
    
    BFBusinessAuthorTableViewCell *cell = [[BFBusinessAuthorTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:BFBusinessAuthorCellIndetifier];
    
    cell.picImageView.tag = indexPath.row +50;
    cell.nameLab.text = self.nameArr[indexPath.row];
    cell.delegate = self;
    NSString *index = [NSString stringWithFormat:@"%ld",indexPath.row];
    UIImage *image = self.imgDic[index];
    if (image != nil) {
     cell.picImageView.image = image;

    }
   
    return cell;
    
    
}
//上传图片的点击事件
- (void)didClickButton:(UIButton *)button{
  BFBusinessAuthorTableViewCell *cell = (BFBusinessAuthorTableViewCell *)button.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    UIImageView *imgView = (UIImageView *)[cell  viewWithTag:indexPath.row +50];
   
    [takePhoto sharePicture:^(UIImage *image) {
       imgView.image = image;
        
        NSString *index = [NSString stringWithFormat:@"%ld",indexPath.row];
        [_imgDic setValue:imgView.image forKey:index];
        [self.tableView reloadData];
 //上传图片的操作
//
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        [params setObject:[BFUserSignelton shareBFUserSignelton].token forKey:@"token"];
        [params setObject:[NSString stringWithFormat:@"%ld",indexPath.row+1] forKey:@"photoid"];
        [params setObject:imgView.image forKey:@"file"];
        [BFAuthorAServicces uploadImagesWithUrl:[NSString stringWithFormat:@"%@%@",BaseURL,@"/MerchantInfo/upload_photo"] image:image params:params success:^(id responseObj) {
//            BFLog(@"responseObj===%@", responseObj);
        

        } failure:^(NSError *error) {
            
        }];

//
    }];

}


//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 60*4;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 180;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 8*60+45;
//}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//
//    return _headerView;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
